package com.jhws.sys.cms.controller;

import com.alibaba.fastjson.JSON;
import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.business.customer.bo.CustomContentInfo;
import com.jhws.business.local.service.LocalService;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.util.GetId;
import com.jhws.common.base.bo.Global;
import com.jhws.common.PropertiesReader;
import com.jhws.core.exception.BizException;
import com.jhws.core.push.PushXinGe;
import com.jhws.business.comm.service.ITblBuildingDimService;
import com.jhws.sys.cms.service.CmsRedisService;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.sys.cms.service.ResourceService;
import com.jhws.sys.cms.bo.Resource;
import com.jhws.sys.cms.bo.ResourceCustomType;
import com.jhws.sys.cms.bo.ResourceSort;
import com.jhws.sys.cms.bo.ResourceType;
import com.jhws.util.GsonUtil;
import com.jhws.util.ObjectUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 简单cms实现
 * 1、需要一个页面用来管理和创建分类，比如分类列表和分类添加，分类列表最好用treeTable展示，能显示出层级关系，类似jeesite的'栏目管理'和'栏目添加'，树形控件LayUI也支持。
 * 2、需要一个页面来管理和创建属于某个分类的链接条目，内容包括上述的'主键', '分类'(标签), '标题', '图片', '内容地址', '创建时间',
 * '创建者',
 *
 * @author xiaokek
 * @since 2016年12月18日 上午12:00:41
 */
@SuppressWarnings("Duplicates")
@Controller
@RequestMapping("/cms/resource")
public class ResourceCtrl {

    @javax.annotation.Resource
    private ResourceService rs;
    @javax.annotation.Resource
    protected ITblUserDimService IUserService;
    @javax.annotation.Resource
    protected com.jhws.business.comm.service.ITblCommDimService ITblCommDimService;
    @javax.annotation.Resource
    private ITblBuildingDimService iTblBuildingDimService;
    @javax.annotation.Resource
    private CmsRedisService redisService;
    @javax.annotation.Resource
    private LocalService localService;
    @javax.annotation.Resource
    private OSSClientService ossClientService;

    /**
     * 资源类型页面
     *
     * @return
     */
    @RequestMapping("typeEdit.x")
    @RequiresRoles("admin")
    public ModelAndView typeEdit() {
        ModelAndView mv = new ModelAndView();
        List<Map<String, Object>> nodes = rs.queryTypes(null);
        mv.addObject("nodes", JSON.toJSON(nodes));
        mv.addObject("roots", rs.queryTypesTree(null));
        mv.setViewName("business/cms/typeEdit");
        return mv;
    }

    /**
     * 查看资源页面
     *
     * @return
     */
    @RequestMapping("resourceList.x")
    @RequiresRoles("admin")
    public ModelAndView resourceList() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("business/cms/resourceList");
        return mv;
    }

    /**
     * 返回添加资源页面
     *
     * @param r
     * @return
     */
    @RequestMapping("goResourceAddPage.x")
    public ModelAndView goResourceAddPage(Resource r) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("types", rs.queryTypesTree(null));
        mv.addObject("resource", GsonUtil.toJson(r));
        mv.setViewName("business/cms/resourceAdd");
        return mv;
    }

    /**
     * 保存资源类型
     *
     * @param rt 资源类型
     * @return
     */
    @RequestMapping("saveType.x")
    @ResponseBody
    public JsonRespData saveType(ResourceType rt,
                                 String[] customPropertyKeys,
                                 String[] customPropertyValues,
                                 String[] customPropertyDescribes) {

        if (customPropertyKeys != null && customPropertyValues != null) {
            if (customPropertyKeys.length != customPropertyValues.length) {
                return new JsonRespData("", -1, "自定义内容错误！");
            }

            //如果有自定义类容转为json
            List<CustomContentInfo> customContentInfos = new ArrayList<>();
            CustomContentInfo contentInfo;
            for (int i = 0; i < customPropertyKeys.length; i++) {
                contentInfo = new CustomContentInfo();
                if (customPropertyDescribes != null && customPropertyDescribes.length > i && StringUtils.isNotEmpty(customPropertyDescribes[i])) {
                    contentInfo.setCustomDescribe(customPropertyDescribes[i]);
                }
                contentInfo.setCustomKey(customPropertyKeys[i]);
                contentInfo.setCustomValue(customPropertyValues[i]);
                if (!StringUtils.isEmpty(customPropertyValues[i])) {
                    customContentInfos.add(contentInfo);
                }
            }

            if (customContentInfos.size() > 0) {
                String customProperty = JSON.toJSONString(customContentInfos);
                rt.setCustomProperty(customProperty);
            }
        }

        rs.saveType(rt);
        return new JsonRespData("", 1, "");
    }

    /**
     * 删除资源类型
     *
     * @param rt
     * @param sortCodes
     * @return
     */
    @RequestMapping("deleteType.x")
    @ResponseBody
    public JsonRespData deleteType(ResourceType rt, String[] sortCodes) {
        rs.deleteType(rt);
        //删除类型
        if (sortCodes != null) {
            for (String sortCode : sortCodes) {
                ResourceSort resourceSort = new ResourceSort();
                resourceSort.setSortCode(sortCode);
                rs.deleteResourceSort(resourceSort);
            }
        }
        return new JsonRespData("", 1, "");
    }

    /**
     * 查询资源类型
     *
     * @return
     */
    @RequestMapping("queryType.x")
    @ResponseBody
    public JsonRespData queryType() {
        Map<String, Object> data = new HashedMap();
        List<Map<String, Object>> nodes = rs.queryTypes(null);
        List<Map<String, Object>> roots = rs.queryTypesTree(null);
        data.put("nodes", nodes);
        data.put("roots", roots);
        return new JsonRespData("", 1, data);
    }

    /**
     * 添加自定义类型页面
     *
     * @return 义类型页面
     */
    @RequestMapping("addCustomType.x")
    public ModelAndView addCustomType(String customTypeId, String type, boolean isNotEdit) throws Exception {
        ModelAndView mv = new ModelAndView();
        if (StringUtils.isNotEmpty(customTypeId)) {
            Map<String, Object> mapP = new HashedMap();
            mapP.put("customTypeId", customTypeId);
            Map<String, Object> resourceCustomTypeMap = rs.queryResourceCustomType(mapP);
            List list = (List) resourceCustomTypeMap.get("list");
            if (list != null && list.size() == 1) {
                mv.addObject("resourceCustomType", list.get(0));
            }
        }
        mv.addObject("isNotEdit", isNotEdit);
        mv.addObject("type", type);
        mv.setViewName("business/cms/addCustomType");
        return mv;
    }

    /**
     * 自定义类型页面
     *
     * @return 义类型页面
     */
    @RequestMapping("customTypeList.x")
    public ModelAndView customTypeList(String type) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("type", type);
        mv.setViewName("business/cms/customTypeList");
        return mv;
    }

    /**
     * 保存自定义类型
     *
     * @return
     */
    @RequestMapping("saveCustomType.x")
    @ResponseBody
    public JsonRespData saveCustomType(ResourceCustomType customType, String[] customNames, String[] customKeys,
                                       String[] spinnerMaps, String[] sortNames, String[] sortCodes) {
        //如果有自定义类容转为json
        List<CustomContentInfo> customContentInfos = new ArrayList<>();
        CustomContentInfo contentInfo;
        if (customNames != null && customKeys != null) {
            if (customNames.length != customKeys.length) {
                return new JsonRespData("", -1, "自定义内容错误！");
            }
            for (int i = 0; i < customNames.length; i++) {
                contentInfo = new CustomContentInfo();
                contentInfo.setCustomType(Global.CMS_CUSTOM_TEXT);
                contentInfo.setCustomName(customNames[i]);
                contentInfo.setCustomKey(customKeys[i]);
                customContentInfos.add(contentInfo);
            }
        }
        if (spinnerMaps != null && sortNames != null && sortCodes != null) {
            if (spinnerMaps.length != sortNames.length) {
                return new JsonRespData("", -1, "自定义选择类型错误！");
            }
            for (int i = 0; i < spinnerMaps.length; i++) {
                Map spinnerMap = JSON.parseObject(spinnerMaps[i], Map.class);
                contentInfo = new CustomContentInfo();
                contentInfo.setCustomType(Global.CMS_CUSTOM_SORT);
                contentInfo.setSpinnerMap(spinnerMap);
                contentInfo.setCustomKey(sortCodes[i]);
                contentInfo.setCustomName(sortNames[i]);
                customContentInfos.add(contentInfo);
            }
        }
        if (customContentInfos.size() > 0) {
            String customContent = com.alibaba.fastjson.JSON.toJSONString(customContentInfos);
            customType.setCustomContent(customContent);
            System.out.println(customContent);
        }

        String customTypeId = rs.saveResourceCustomType(customType);
        return new JsonRespData("", 1, customTypeId);
    }

    /**
     * 查询自定义类型
     *
     * @return
     */
    @RequestMapping("queryCustomTypeList.x")
    @ResponseBody
    public JsonRespData queryCustomTypeList(@RequestParam Map param) throws Exception {
        Map<String, Object> resourcesCustomType = rs.queryResourceCustomType(param);
        return new JsonRespData("", 1, resourcesCustomType);
    }

    /**
     * 删除自定义类型
     *
     * @return
     */
    @RequestMapping("deleteCustomType.x")
    @ResponseBody
    public JsonRespData deleteCustomType(ResourceCustomType customType) throws Exception {
        rs.deleteCustomType(customType);
        return new JsonRespData("", 1, "删除成功");
    }

    /**
     * 查询资源
     *
     * @param param
     * @param includeSub
     * @return
     * @throws IOException
     */
    @RequestMapping("queryResourceList.x")
    @ResponseBody
    public JsonRespData queryResourceList(@RequestParam Map param, boolean includeSub) throws IOException {
        Map<String, Object> resources = rs.queryResourcePage(includeSub, param);
        return new JsonRespData("", 1, resources);
    }

    /**
     * 上传文件
     *
     * @param file    文件头
     * @param request
     * @param isTemp  是否放入临时文件
     * @return
     * @throws IOException
     */
    @RequestMapping("uploadFile.x")
    @ResponseBody
    public JsonRespData uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request, boolean isTemp) throws IOException {
        String path = rs.uploadRemoteFile(file, null, isTemp);
        String url = "";
        url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                + request.getContextPath();
        if (path != null) {
            url += "/cms/resource/downloadRemoteFile.x?path=" + path;
        }
        Map<String, String> ret = new HashMap<>();
        ret.put("imagePath", path);
        ret.put("imageUrl", url);
        return new JsonRespData("", 1, ret);
    }

    /**
     * 下载oss文件
     *
     * @param path
     * @param resp
     * @throws IOException
     */
    @RequestMapping("downloadRemoteFile.x")
    public void downloadRemoteFile(@RequestParam("path") String path, HttpServletResponse resp) throws IOException {
        String url = ossClientService.getUrl(path, false);
        resp.sendRedirect(url);
    }

    /**
     * 下载文件
     *
     * @param path 路径
     * @return
     * @throws IOException
     */
    @RequestMapping("downloadFile.x")
    @ResponseBody
    public HttpEntity<byte[]> downloadFile(@RequestParam("path") String path)
            throws IOException {
        return rs.download(path);
    }

    /**
     * 保存资源
     *
     * @param request
     * @param r                   资源
     * @param customDescribes     自定义描述
     * @param customKeys          自定义key
     * @param customValues        自定义value
     * @param sortCustomDescribes 自定义选择描述
     * @return
     * @throws IOException
     */
    @RequestMapping("saveResource.x")
    @ResponseBody
    public JsonRespData saveResource(HttpServletRequest request,
                                     Resource r,
                                     String[] customDescribes,
                                     String[] customKeys,
                                     String[] customValues,
                                     String[] sortCustomDescribes) throws IOException {
        TblUserDimBo userBo = IUserService.currentUser();
        //如果有自定义类容转为json
        List<CustomContentInfo> customContentInfos = new ArrayList<>();
        CustomContentInfo contentInfo;
        if (customKeys != null && customValues != null) {
            if (customValues.length != customKeys.length) {
                new JsonRespData("", -1, "自定义内容错误！");
            }
            for (int i = 0; i < customKeys.length; i++) {
                contentInfo = new CustomContentInfo();
                if (customDescribes != null && customDescribes.length > i && StringUtils.isNotEmpty(customDescribes[i])) {
                    contentInfo.setCustomDescribe(customDescribes[i]);
                }
                contentInfo.setCustomKey(customKeys[i]);
                contentInfo.setCustomValue(customValues[i]);
                if (!StringUtils.isEmpty(customValues[i])) {
                    customContentInfos.add(contentInfo);
                }
            }
        }

        if (customContentInfos.size() > 0) {
            String customContent = com.alibaba.fastjson.JSON.toJSONString(customContentInfos);
            r.setCustomContent(customContent);
        }
        //将临时文件拷贝
        String imagePath = rs.copyRemoteFile(r.getIcon_url(), GetId.randomID());
        r.setIsApproved(Global.CMS_IS_APPROVED);
        r.setApprovedState("正在审核");
        r.setCreator(userBo.getLogName());
        r.setIcon_url(imagePath);
        rs.saveResource(r);
        return new JsonRespData("", 1, "");
    }

    /**
     * 删除资源
     *
     * @param r
     * @param isNotDeleteOss
     * @return
     */
    @RequestMapping("deleteResource.x")
    @ResponseBody
    public JsonRespData deleteResource(Resource r, boolean isNotDeleteOss) {
        Resource resource = rs.queryResourceIdByResource(r.getResource_id());
        //如果oss上的资源被删除，就删除所以相关的资源条目
        if (!isNotDeleteOss && resource.getIcon_url() != null && !ObjectUtils.isHttpAddress(resource.getIcon_url())) {
            //先删除oss上的文件
            rs.deleteRemoteFile(resource.getIcon_url());
        } else {
            rs.deleteResource(r);
        }
        return new JsonRespData("", 1, "");
    }

    /**
     * app主页页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("appInterfaceCustomization.x")
    public ModelAndView appInterfaceCustomization() throws Exception {
        ModelAndView mv = new ModelAndView();
        Object roots = com.alibaba.fastjson.JSON.toJSON(rs.queryTypesTree("appContent"));
        mv.addObject("roots", roots);
        //如果是超级管理员可以审核资源
        if (SecurityUtils.getSubject().hasRole(Global.ROLE_SUPPER_ADMIN)) {
            mv.addObject("isCheck", true);
        }
        Map<String, Object> param = new HashedMap();
        param.put("sortCode", "appOperationType");
        param.put("isPage", false);
        Map<String, Object> resourcesSort = rs.queryResourceSortPage(param);
        mv.addObject("resourcesSorts", resourcesSort);

        mv.setViewName("business/cms/appContent");
        return mv;
    }

    /**
     * @return 返回分配广告弹框
     */
    @RequestMapping("assignsResource.x")
    public ModelAndView assignsResource() throws IOException {
        ModelAndView mv = new ModelAndView();
        Object roots = com.alibaba.fastjson.JSON.toJSON(rs.queryTypesTree("appContent"));
        mv.addObject("roots", roots);
        mv.setViewName("business/cms/assignsResource");
        return mv;
    }

    /**
     * @return 返回添加类型弹出框
     */
    @RequestMapping("addResourceSort.x")
    public ModelAndView addResourceSort() throws IOException {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("business/cms/addResourceSort");
        return mv;
    }

    /**
     * 保存临时数据类型，防止用户中途取消导致的冗余数据
     *
     * @return 是否保存成功
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("saveResourceSort.x")
    @ResponseBody
    public JsonRespData saveResourceSort(ResourceSort resourceSort, String[] displayNames, String[] sortValues, String saveType) {
        //如果是修改类型，先删除以前的类型
        if ("modify".equals(saveType)) {
            rs.deleteResourceSort(resourceSort);
        } else if (rs.isResourceSortCode(resourceSort.getSortCode())) {
            throw new BizException("类型编码已经存在！");
        }

        if (displayNames == null || sortValues == null || displayNames.length != sortValues.length) {
            return new JsonRespData("", -1, "自定义类型错误！");
        }

        for (int i = 0; i < displayNames.length; i++) {
            resourceSort.setSortId("");
            resourceSort.setDisplayName(displayNames[i]);
            resourceSort.setSortValue(sortValues[i]);
            rs.saveResourceSort(resourceSort);
        }
        return new JsonRespData("", 1, resourceSort);
    }


    /**
     * 自定义选择列表
     *
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping("queryResourceSortList.x")
    @ResponseBody
    public JsonRespData queryResourceSortList(@RequestParam Map param) throws Exception {
        Map<String, Object> resourcesSort = rs.queryResourceSortPage(param);
        return new JsonRespData("", 1, resourcesSort);
    }

    /**
     * 删除自定义选择
     *
     * @param resourceSort
     * @return
     */
    @RequestMapping("deleteResourceSort.x")
    @ResponseBody
    public JsonRespData deleteResourceSort(ResourceSort resourceSort) {
        rs.deleteResourceSort(resourceSort);
        return new JsonRespData("", 1, "");
    }

    /**
     * 功能：将资源分配给相应的小区
     *
     * @param resourceCodes 资源ID
     * @param commCodes     小区列表
     * @return 成功或者失败
     * @throws Exception
     */
    @RequestMapping("submitAssignsResource.x")
    @ResponseBody
    public JsonRespData submitAssignsResource(String resourceCodes, String commCodes) throws Exception {
        String[] resourceIDs = null;
        if (!StringUtils.isEmpty(resourceCodes)) {
            resourceIDs = resourceCodes.split(",");
        }
        String[] commCodeArray = commCodes.split(",");
        for (String commCode : commCodeArray) {
            if (StringUtils.isEmpty(commCode)) {
                continue;
            }
            //先删除原有已经分配的资源
            List<Map<String, Object>> nodes = rs.queryTypesTree("appContent");
            for (Map<String, Object> map : nodes) {
                String typeCode = MapUtils.getString(map, "type_code", "");
                rs.deleteResource(typeCode, commCode);
            }
            //分配资源
            rs.updateResource(null, resourceIDs, commCode);
        }
        return new JsonRespData("", 1, "");
    }

    /**
     * 审核状态
     *
     * @param resource_id   资源Id
     * @param isApproved    是否通过
     * @param approvedState 状态原因
     * @param isUpdateTime  更新时间
     * @return
     * @throws Exception
     */
    @RequestMapping("approvedState.x")
    @ResponseBody
    public JsonRespData approvedState(String resource_id, String isApproved, String approvedState, boolean isUpdateTime) throws Exception {
        Resource resource = rs.queryResourceIdByResource(resource_id);
        resource.setResource_id(resource_id);
        resource.setIsApproved(isApproved);
        if (!Global.CMS_APPROVED.equals(isApproved)) {
            approvedState = "未通过审核\n原因：" + approvedState;
        }
        resource.setApprovedState(approvedState);
        if (isUpdateTime) {
            resource.setCreate_time(new Date());
        }
        rs.saveResource(resource);
        return new JsonRespData("", 1, "");
    }

    /**
     * 门口机广告页面
     *
     * @return
     */
    @RequestMapping("outdoorAdvAllocate.x")
    public ModelAndView outdoorAdvAllocate() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("business/cms/outdoorAdvertisement");
        return mv;
    }

    /**
     * 选择门口机广告页面
     *
     * @param resourceId
     * @param commCode
     * @param fileName
     * @return
     */
    @RequestMapping("redistribution.x")
    public ModelAndView redistribution(String resourceId, String commCode, String fileName) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("resourceId", resourceId);
        mv.addObject("commCode", commCode);
        mv.addObject("fileName", fileName);
        mv.setViewName("business/cms/redistribution");
        return mv;
    }

    /**
     * 替换门口机广告
     *
     * @param resourceId 资源Id
     * @param commCode   小区
     * @param fileName   文件名
     * @param tempPath   临时文件路径
     * @return
     * @throws IOException
     */
    @RequestMapping("redistributionOutdoorAdv.x")
    @ResponseBody
    public JsonRespData redistributionOutdoorAdv(@RequestParam("file") MultipartFile file,
                                                 String resourceId,
                                                 String commCode,
                                                 String fileName,
                                                 String screenType,
                                                 String tempPath) throws IOException {
        TblUserDimBo userBo = IUserService.currentUser();
        Resource resource = null;
        if (!StringUtils.isEmpty(resourceId)) {
            resource = rs.queryResourceIdByResource(resourceId);
        }
        if (resource == null) {
            resource = new Resource();
        }
        String newFileName = "comm_" + commCode + "_" + fileName;
        //如果局域网模式就不上传oss
        String imagePath = localService.saveAdvImage(file, newFileName, () -> {
            if (file == null) {
                return rs.copyRemoteFile(tempPath, newFileName);
            } else {
                return rs.uploadRemoteFile(file, newFileName, false);
            }
        });
        resource.setCustomType(screenType);
        resource.setComm_code(commCode);
        resource.setType_code("outdoor_image_ads");
        resource.setDescription(fileName);
        resource.setIsApproved(Global.CMS_APPROVED);
        resource.setApprovedState("通过审核");
        resource.setTitle("门口机广告");
        resource.setIcon_url(imagePath);
        resource.setCreator(userBo.getLogName());
        resource.setCreate_time(new Date());
        rs.saveResource(resource);
        return new JsonRespData("", 1, "");
    }

    /**
     * 通过小区获取资源
     *
     * @param param
     * @return
     * @throws IOException
     */
    @RequestMapping("findResourceByComm.x")
    @ResponseBody
    public Object findResourceByComm(@RequestParam Map param) throws IOException {
        Map<String, Object> mapComm = ITblCommDimService.findCommListPage(param);
        List<Map<String, Object>> list = (List) mapComm.get("list");
        for (Map<String, Object> map : list) {
            String commCode = (String) map.get("comm_code");
            String typeCode = (String) param.get("typeCode");//"video_ads";
            List<Resource> resourceList = rs.queryResourceList("where 1=1 and comm_code ='" + commCode + "' and type_code='" + typeCode + "'");
            for (Resource resource : resourceList) {
                if (!StringUtils.isEmpty(resource.getIcon_url()) && !ObjectUtils.isHttpAddress(resource.getIcon_url())) {
                    String url = ossClientService.getUrl(resource.getIcon_url(), false);
                    resource.setIcon_url(url);
                }
            }
            map.put("resourceList", resourceList);
        }
        return new JsonRespData("", 1, mapComm);
    }

    /**
     * 通知更新
     *
     * @param commCode 小区
     * @return
     * @throws Exception
     */
    @RequestMapping("notificationUpdate.x")
    @ResponseBody
    public Object notificationUpdate(String commCode) throws Exception {
        List<String> owList = iTblBuildingDimService.findAllOutdoorOrWallMachineByComm(commCode);
        for (String sipNum : owList) {
            PushXinGe.androidPushSingleAccount(sipNum, "notificationUpdate", com.alibaba.fastjson.JSON.toJSONString(new JsonRespData("notificationUpdate", 1, "通知成功")));
        }
        return new JsonRespData("notificationUpdate", 1, "通知成功");
    }

    @RequestMapping("outdoorVideoAds.x")
    public ModelAndView outdoorVideoAds() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("business/cms/outdoorVideoAds");
        return mv;
    }

    /**
     * 上传视频广告
     *
     * @param file       文件头
     * @param request
     * @param resourceId 资源Id
     * @param commCode   小区
     * @param fileName   文件名
     * @return
     * @throws IOException
     */
    @SuppressWarnings("ResultOfMethodCallIgnored")
    @RequestMapping("uploadVideoAds.x")
    @ResponseBody
    public JsonRespData uploadVideoAds(@RequestParam("file") MultipartFile file, HttpServletRequest request, String resourceId, String commCode, String fileName) throws IOException {
        //创建你要保存的文件的路径
        String path = request.getSession().getServletContext().getRealPath("upload");
        String suffix = FilenameUtils.getExtension(file.getOriginalFilename());
        //需要保存的文件名
        String saveFileNama = commCode + "_" + fileName + "." + suffix;
        File targetFile = new File(path, saveFileNama);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        // 保存
        file.transferTo(targetFile);
        TblUserDimBo userBo = IUserService.currentUser();
        Resource resource = null;
        if (!StringUtils.isEmpty(resourceId)) {
            resource = rs.queryResourceIdByResource(resourceId);
        }
        if (resource == null) {
            resource = new Resource();
        }
        String serverName = request.getServerName();
        if (!PropertiesReader.getProperty("isLanMode").equals("false")) {
            serverName = "192.168.1.2";
        }
        String url = request.getScheme() + "://" + serverName + ":" + request.getServerPort() + request.getContextPath() + "/upload/" + saveFileNama;
        resource.setType_code("video_ads");
        resource.setDescription(fileName);
        resource.setIsApproved(Global.CMS_APPROVED);
        resource.setApprovedState("通过审核");
        resource.setTitle("门口机视频");
        resource.setContent_url(url);
        resource.setIcon_url("");
        resource.setCreator(userBo.getLogName());
        resource.setCreate_time(new Date());
        resource.setComm_code(commCode);
        rs.saveResource(resource);
        return new JsonRespData("", 1, "上传成功");
    }

    @RequestMapping("clearCache.x")
    @ResponseBody
    public JsonRespData clearCache() {
        //清空缓存
        redisService.emptyRedisCMSAll();
        redisService.emptyRedisCommGroupAll();
        return new JsonRespData("", 1, "成功");
    }
}
